//
//  UserInfo.swift
//  HuaYun
//
//  Created by enghou on 2018/7/31.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import Foundation

import KeychainSwift

import AdSupport

class User: NSObject {
    
    static func clear()->Void{
        KeychainSwift.init().clear()
    }
    
    static func canRequestReiment()->Bool{
        return self.role == "LABOURSERVICESOUTSOURCING"
    }
    
    static var role: String {
        return KeychainSwift.init().get("role") ?? ""
    }
    
    static func configUUID(uuid: String) {
        KeychainSwift.init().set(uuid, forKey: "uuid")
    }
    
    static var uuid: String{
        let uid = KeychainSwift.init().get("uuid") ?? ""
        if uid.count > 0 {
            return uid
        }
        let id = ASIdentifierManager.shared().advertisingIdentifier.uuidString
        self.configUUID(uuid: id)
        return id
    }
    
    static func configRole(role: String)->Void{
        KeychainSwift.init().set(role, forKey: "role")
    }
    
    static func configId(id: String)->Void{
        KeychainSwift.init().set(id, forKey: "id")
    }
    
    static var id: String {
        return KeychainSwift.init().get("id") ?? ""
    }
    
    static func configName(name: String)->Void{
        KeychainSwift.init().set(name, forKey: "name")
    }
    
    static var account: String {
        
        return KeychainSwift.init().get("account") ?? ""
        
    }
    
    static func configAccount(account: String)->Void{
        KeychainSwift.init().set(account, forKey: "account")
    }
    
    static var name: String {
        return KeychainSwift.init().get("name") ?? ""
    }
    
    static func configPhone(phone: String)->Void{
        KeychainSwift.init().set(phone, forKey: "phone")
    }
    
    static var phone: String {
        return KeychainSwift.init().get("phone") ?? ""
    }
    
    static func configOrgCode(orgCode: String)->Void{
        KeychainSwift.init().set(orgCode, forKey: "orgcode")
    }
    
    static var orgCode: String {
        if self.ifAdmin {
            return "x"
        } else {
            return KeychainSwift.init().get("orgcode") ?? ""
        }
    }
    
    static var deviceNumber: String{
        return KeychainSwift.init().get("deviceNumber") ?? ""
    }
    
    static func configDeviceNumber(devicenum: String)->Void{
        KeychainSwift.init().set(devicenum, forKey: "deviceNumber")
    }
    
    static var canEnter: Bool {
        return self.isLeader
    }
    
    static func configIfAdmin(ifadmin: Bool)->Void{
        KeychainSwift.init().set(ifadmin, forKey: "ifadmin")
    }
    
    static var ifAdmin: Bool{
        return KeychainSwift.init().getBool("ifadmin") ?? false
    }
    
    static func configScan(ifscan: Bool){
//        KeychainSwift.init().set(ifadmin, forKey: "ifscan")
        KeychainSwift.init().set(ifscan, forKey: "ifscan")
    }
    
    static var scan: Bool{
        if self.ifAdmin == true {
            return true
        }
        if let ifscan = KeychainSwift.init().getBool("ifscan") {
            return ifscan
        } else {
            return false
        }
    }
    
    
    static var isLeader: Bool{
        let orgs = ["x","x1x"]
        return (orgs.firstIndex(of: self.orgCode) != nil) || self.ifAdmin
    }
    
}
